package m.jclecai.com.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;
import m.jclecai.com.R;
import m.jclecai.com.base.recyclerview.BaseRecyclerViewAdapter;
import m.jclecai.com.base.recyclerview.BaseViewHolder;
import m.jclecai.com.bean.common.ShareBean;

import static m.jclecai.com.utils.PackageCompareUtil.getIconResourceByPackage;

/**
 * Created by andy on 2017/7/17.
 *
 * 这里是分享的Didlog;
 */

public class ShareDialog {

    private final Context mContext;
    public RecyclerView mRecyclerView;

    private List<ShareBean> mShareList;
    private MyAdapter mShareAdapter;
    private PlatformActionListener mListener;
    private Dialog mDialog;


    public ShareDialog(Activity context) {

        mContext = context;
        initView();
        initData();

    }

    private void initData() {
        mShareList = new ArrayList<>();
        mShareList.add(new ShareBean(R.drawable.ic_tg_db_wx,"微信", Wechat.NAME));
        mShareList.add(new ShareBean(R.drawable.ic_tg_db_pyq,"朋友圈", WechatMoments.NAME));
        mShareAdapter.refresh(mShareList);
    }

    private void initView() {


        mDialog = new Dialog(mContext, R.style.dialog_share);

        Window window = mDialog.getWindow();
        window.setGravity(Gravity.BOTTOM);
        View view1 = View.inflate(mContext, R.layout.layout_share_dialog, null);

        mRecyclerView = ((RecyclerView) view1.findViewById(R.id.rv_share));
        mRecyclerView.setLayoutManager(new GridLayoutManager(mContext,4));

        mShareAdapter=new MyAdapter(null);
        mRecyclerView.setAdapter(mShareAdapter);

        mDialog.setContentView(view1);

        WindowManager systemService = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
        int width = systemService.getDefaultDisplay().getWidth();
        //将对话框的宽度设置为屏幕的宽度；
        WindowManager.LayoutParams attributes = window.getAttributes();
        attributes.width=width;

        window.setAttributes(attributes);
    }

    public  class  MyAdapter extends BaseRecyclerViewAdapter<ShareBean>{

        public MyAdapter(List<ShareBean> DList) {
            super(DList);
        }

        @Override
        public int getItemView() {
            return R.layout.item_share;
        }

        @Override
        protected void bindViewHolder(BaseViewHolder holder, final ShareBean bean, int position) {
            holder.getTextView(R.id.tv_share_text).setText(bean.getDrawableDesc());
            holder.getImageView(R.id.iv_share_icon).setImageResource(bean.getDrawableResId());
            //这里我们需要执行不同的逻辑
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //之类我们要进行不同的跳转；
                    mDialog.dismiss();
                    share(bean.getShareType());
                }
            });
        }
    }

    public  void show(){
        mDialog.show();
    }

    public void dismiss(){
        mDialog.dismiss();
    }


    public void share(String platformName) {

        Platform.ShareParams wechatMoments = new Platform.ShareParams();
        wechatMoments.setTitle("乐彩神彩票");
        wechatMoments.setText("快点来加入我们吧！！！");
        wechatMoments.setUrl("http://m.jclecai.com/");
        // wechatMoments.setImageUrl("http://f1.sharesdk.cn/imgs/2014/02/26/owWpLZo_638x960.jpg");

        wechatMoments.setImageData(BitmapFactory.decodeResource(mContext.getResources(),getIconResourceByPackage()
        ));
        wechatMoments.setShareType(Platform.SHARE_WEBPAGE);
        Platform weixin = ShareSDK.getPlatform(platformName);

        if(mListener!=null){
            weixin.setPlatformActionListener(mListener);
        }else{
            weixin.setPlatformActionListener(new PlatformActionListener() {
                @Override
                public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {

                    // DisPlay("分享成功");
                    Toast.makeText(mContext,"分享成功",Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onError(Platform platform, int i, Throwable throwable) {

                    // DisPlay("分享失败");
                    Toast.makeText(mContext,"分享失败",Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onCancel(Platform platform, int i) {

                    // DisPlay("分享取消");
                    Toast.makeText(mContext,"分享取消",Toast.LENGTH_SHORT).show();
                }
            });
        }
        weixin.share(wechatMoments);
    }

    public void setShareListener(PlatformActionListener listener) {
        this.mListener = listener;
    }
}
